package com.dianshang.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dianshang.dao.OrderItemDao;
import com.dianshang.dao.ReturnOrderDao;
import com.dianshang.dao.ReturnOrderItemDao;
import com.dianshang.entity.PageResult;
import com.dianshang.pojo.order.OrderItem;
import com.dianshang.pojo.order.ReturnOrderDetail;
import com.dianshang.pojo.order.ReturnOrder;
import com.dianshang.pojo.order.ReturnOrderItem;
import com.dianshang.service.order.ReturnOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Service(interfaceClass = ReturnOrderService.class)
 * 注意！！！这个方法要对两个表进行操作,需要添加事务。
 */
@Service(interfaceClass = ReturnOrderService.class)
public class ReturnOrderServiceImpl implements ReturnOrderService {

    @Autowired
    private ReturnOrderDao returnOrderDao;

    @Autowired
    private ReturnOrderItemDao returnOrderItemDao;

    @Autowired
    private OrderItemDao orderItemDao;

    /**
     * 查询所有
     */
    @Override
    public List<ReturnOrder> findAll() {
        return returnOrderDao.selectAll();
    }

    /**
     * 查询所有+分页
     * page 页码
     * size 每页记录数
     * 分页结果
     */
    @Override
    public PageResult<ReturnOrder> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<ReturnOrder> returnOrders = (Page<ReturnOrder>) returnOrderDao.selectAll();
        return new PageResult<ReturnOrder>(returnOrders.getTotal(),returnOrders.getResult());
    }

    /**
     * 查询所有+模糊
     * searchMap 查询条件
     */
    @Override
    public List<ReturnOrder> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return returnOrderDao.selectByExample(example);
    }

    /**
     * 查询所有+分页+模糊
     * searchMap
     * page
     * size
     */
    @Override
    public PageResult<ReturnOrder> findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        PageHelper.startPage(page,size);

        Example example = new Example(ReturnOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap.get("id") != null && !searchMap.get("id").equals("")){
            criteria.andEqualTo("id",searchMap.get("id"));
        }

        Page<ReturnOrder> returnOrders = (Page<ReturnOrder>) returnOrderDao.selectByExample(example);
        return new PageResult<ReturnOrder>(returnOrders.getTotal(),returnOrders.getResult());
    }

    /**
     * 退款组合表
     * id
     */
    @Override
    @Transactional // 事务会覆盖类的事务配置信息
    public ReturnOrderDetail findById(Integer id) {
        // 查询return_order列表
        ReturnOrder returnOrder = returnOrderDao.selectByPrimaryKey(id);
        // 查询return_order_item列表
        Example example = new Example(ReturnOrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("returnOrderId", id);
        List<ReturnOrderItem> returnOrderItems = returnOrderItemDao.selectByExample(example);
        // 封装为组合实体类
        ReturnOrderDetail returnOrderDetail = new ReturnOrderDetail();
        returnOrderDetail.setReturnOrder(returnOrder);
        returnOrderDetail.setReturnOrderItemList(returnOrderItems);
        return returnOrderDetail;
    }

    /**
     * 新增
     * returnOrder
     */
    @Override
    public void add(ReturnOrder returnOrder) {
        returnOrderDao.insert(returnOrder);
    }

    /**
     * 修改
     * returnOrder
     */
    @Override
    public void update(ReturnOrder returnOrder) {
        returnOrderDao.updateByPrimaryKeySelective(returnOrder);
    }

    /**
     * 删除
     * id
     */
    @Override
    public void delete(Integer id) {
        returnOrderDao.deleteByPrimaryKey(id);
    }

    /**
     * 同意退款
     */
    @Override
    public void agreeRefund(String id) {
        ReturnOrder returnOrder = returnOrderDao.selectByPrimaryKey(id);
        if (returnOrder==null){
            throw new RuntimeException("退款订单不存在");
        }
        if (!returnOrder.getType().equals("2")){
            throw new RuntimeException("不是退款订单");
        }
        // if (money>returnOrder.getReturnMoney()||money<0){
        //     throw new RuntimeException("退款金额不合法！");
        // }
        // returnOrder.setReturnMoney(money); // 退款金额
        returnOrder.setStatus("1"); // 申请状态 0:申请,1:同意,2:驳回
        returnOrder.setDisposeTime(new Date()); // 处理时间
        returnOrderDao.updateByPrimaryKeySelective(returnOrder);
    }

    /**
     * 拒绝/驳回退款
     */
    @Override
    @Transactional // 事务会覆盖类的事务配置信息
    public void rejectRefund(String id) {
        ReturnOrder returnOrder = returnOrderDao.selectByPrimaryKey(id);
        if (returnOrder == null && !returnOrder.getType().equals("2")){ // 类型 1.退货,2.退款
            throw new RuntimeException("退款订单不存在/不是退款订单");
        }
        // if (remark.length()<5){
        //     throw new RuntimeException("请输入驳回理由");
        // }
        // returnOrder.setRemark(remark); // 驳回理由/处理备注
        returnOrder.setStatus("2"); // 申请状态 0:申请,1:同意,2:驳回
        returnOrder.setDisposeTime(new Date()); // 处理日期
        returnOrderDao.updateByPrimaryKeySelective(returnOrder); // 保存

        Example example = new Example(ReturnOrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("returnOrderId", id);
        List<ReturnOrderItem> returnOrderItems = returnOrderItemDao.selectByExample(example);
        for (ReturnOrderItem returnOrderItem : returnOrderItems) {
            OrderItem orderItem = new OrderItem();
            orderItem.setId(returnOrderItem.getOrderItemId()+"");
            orderItem.setIsReturn("0"); // 是否退货 0:未退款,1:已申请,2:已退
            orderItemDao.updateByPrimaryKeySelective(orderItem);
        }
    }

    /**
     * 构建查询条件
     * searchMap
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(ReturnOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap != null){
            // 用户账号
            if(searchMap.get("userAccount")!=null && !"".equals(searchMap.get("userAccount"))){
                criteria.andLike("userAccount","%"+searchMap.get("userAccount")+"%");
            }
            // 联系人
            if(searchMap.get("linkman")!=null && !"".equals(searchMap.get("linkman"))){
                criteria.andLike("linkman","%"+searchMap.get("linkman")+"%");
            }
            // 联系人手机
            if(searchMap.get("linkmanMobile")!=null && !"".equals(searchMap.get("linkmanMobile"))){
                criteria.andLike("linkmanMobile","%"+searchMap.get("linkmanMobile")+"%");
            }
            // 类型
            if(searchMap.get("type")!=null && !"".equals(searchMap.get("type"))){
                criteria.andLike("type","%"+searchMap.get("type")+"%");
            }
            // 是否退运费
            if(searchMap.get("isReturnFreight")!=null && !"".equals(searchMap.get("isReturnFreight"))){
                criteria.andLike("isReturnFreight","%"+searchMap.get("isReturnFreight")+"%");
            }
            // 申请状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andLike("status","%"+searchMap.get("status")+"%");
            }
            // 凭证图片
            if(searchMap.get("evidence")!=null && !"".equals(searchMap.get("evidence"))){
                criteria.andLike("evidence","%"+searchMap.get("evidence")+"%");
            }
            // 问题描述
            if(searchMap.get("description")!=null && !"".equals(searchMap.get("description"))){
                criteria.andLike("description","%"+searchMap.get("description")+"%");
            }
            // 处理备注
            if(searchMap.get("remark")!=null && !"".equals(searchMap.get("remark"))){
                criteria.andLike("remark","%"+searchMap.get("remark")+"%");
            }
            // 退款金额
            if(searchMap.get("returnMoney")!=null ){
                criteria.andEqualTo("returnMoney",searchMap.get("returnMoney"));
            }
            // 退货退款原因
            if(searchMap.get("returnCause")!=null ){
                criteria.andEqualTo("returnCause",searchMap.get("returnCause"));
            }
            // 管理员id
            if(searchMap.get("adminId")!=null ){
                criteria.andEqualTo("adminId",searchMap.get("adminId"));
            }
        }
        return example;
    }

}